/*************************************************
 *FileName:      search.js
 *Description:   搜索.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var oerrormsg=true,page=1 ,page_num=3,words='',sex,opent=true,owight;
    
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //加载
        listlode();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        var startx, starty,owinw = $('.box-one').attr('left') , endx, endy; //获得角度
    function getAngle(angx, angy) {
        return Math.atan2(angy, angx) * 180 / Math.PI;
    };
    //根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
    function getDirection(startx, starty, endx, endy) {
        var angx = endx - startx;
        var angy = endy - starty;
        var result = 0;
        //如果滑动距离太短
        if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
            return result;
        }
        var angle = getAngle(angx, angy);
        if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
            result = 3;
        } else if (angle >= -45 && angle <= 45) {
            result = 4;
        }
            return result;
    }   
    
    $(".search").on("touchstart",'.particulars-one', function(e) {//    手指接触屏幕
        startx = e.originalEvent.touches[0].pageX;
        starty = e.originalEvent.touches[0].pageY;
    }).on("touchmove", '.particulars-one',function(e) {//手指离开屏幕
        endx = e.originalEvent.changedTouches[0].pageX;
        $(this).css({marginLeft:(endx-startx)});
    }).on("touchend", '.particulars-one',function(e) {
        endx = e.originalEvent.changedTouches[0].pageX;
        endy = e.originalEvent.changedTouches[0].pageY;
        var direction = getDirection(startx, starty, endx, endy);
        var othis =$(this);
        switch (direction) {
            case 3:
                //向左
                $(this).css({transform:'translate(-160%,0)'});
                setTimeout(function(){
                    $('.particulars-two').addClass('particulars-one').removeClass('particulars-two');
                    $('.particulars-three').addClass('particulars-two').removeClass('particulars-three');
                    othis.removeClass('particulars-one').addClass('particulars-three').removeAttr('style');
                    page++;
                    pagelod(othis);
                },200)
                break;
            case 4:
                $(this).css({transform:'translate(60%,0)'});
                setTimeout(function(){
                    $('.particulars-two').addClass('particulars-one').removeClass('particulars-two');
                    $('.particulars-three').addClass('particulars-two').removeClass('particulars-three');
                    othis.removeClass('particulars-one').addClass('particulars-three').removeAttr('style');
                    page++;
                    pagelod(othis);
                },200)
                //向右
                break;
            default:
                othis.removeAttr('style');
        }
        $('.p-img').removeClass('p-imgh');
        $('.particulars-one').find('.p-img').on('load',function(){
            if($('.particulars-one').find('.p-img').width()<owight){
                $('.particulars-one').find('.p-img').addClass('p-imgh');
            }
        })//判断宽高
    })
        
        $('.recommend-title').on('tap','a',function(){
            $(this).addClass('active').siblings().removeClass('active')
        })
        
        $('.search .a-search').on('tap',function(){
            oerrormsg=true
            serch();
        })
        
        $('.search .search-seek input').on('focusin',function(){
            $('.search .particulars').css({display:'none'});
            $('.search .recommend').css({display:'block'});
        }).on('blur',function(){
            $('.search .particulars').css({display:'block'});
            $('.search .recommend').css({display:'none'});
        })
        
        
        $('.particulars .box').on('tap','.particulars-box img',function(){
            common.pageTransition("personal.html?friend_uid="+$(this).parents('.particulars-box').attr('data-id'))
        })
        
        $('.recommend-box').on('tap','a',function(){
            var otext =$(this).text();
            $('.serch-input').val(otext);
            serch();
        })
        
        function serch(){
            $('input').blur();
            $('.search .recommend').hide();
            $('.search .particulars').show();
            words=$('.search-seek input').val();
            var  osex =$('.recommend-title .active').index();
            sex= osex==1 ? 'man' : osex==2 ? 'woman' :'';
            page=1 ;
            page_num=3;
            pagelod();
        }
    }
    /**
     * pagelod
     * 数据加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagelod(othis){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/getSearchList",
            dataType:'jsonp',
            data: { uid: uid,token:token,sex:sex,words:words,page:page,page_num:page_num},
            success: function(data) {
                if(data.error_code == 0){
                    var ohtml ='';
                    if(!othis){
                        for(var i=0 ; i<3 ; i++){
                            var oboxname = i == 0 ? 'one' : i== 1 ? 'two' : 'three';
                            if(data.data[i]){
                                var odynamic = data.data[i].odynamic ?  '<div class="friends"></div>' : '';
                                var ovip =  data.data[i].vip? '<div class="vip particulars-div">VIP</div>' :'';
                                var otface = data.data[i].face ?  DOMAIN_NAME+data.data[i].face :'images/sherch_bq.png' ;
                                ohtml +='<div data-id="'+data.data[i].uid+'" class="particulars-box particulars-'+oboxname+'"><div class="particulars-img">'
                                        +'<img class="p-img" src="'+otface+'"/><div class="friends-box">'
                                        +'<div class="img-amount">'+data.data[i].files_num+'</div>'+odynamic
                                        +'</div></div><div class="particulars-name">'+data.data[i].nickname+'</div>'
                                        +'<div class="particulars-div particulars-age"><i class="'+data.data[i].sex+'"></i><span>'+data.data[i].age+'岁</span></div>'
                                        +'<div class="particulars-div particulars-meili"><span>'+data.data[i].level+'级魅力</span></div>'
                                        +ovip+'</div>';
                            }else if(i==1||i==2){
                                i==2 ? opent=false : oerrormsg='off' ;
                                var odynamic = data.data[0].odynamic ?  '<div class="friends"></div>' : '';
                                var ovip =  data.data[0].vip? '<div class="vip particulars-div">VIP</div>' :'';
                                var otface = data.data[0].face ?  DOMAIN_NAME+data.data[0].face :'images/sherch_bq.png' ;
                                ohtml +='<div data-id="'+data.data[0].uid+'" class="particulars-box particulars-'+oboxname+'"><div class="particulars-img">'
                                        +'<img class="p-img" src="'+otface+'"/><div class="friends-box">'
                                        +'<div class="img-amount">'+data.data[0].files_num+'</div>'+odynamic
                                        +'</div></div><div class="particulars-name">'+data.data[0].nickname+'</div>'
                                        +'<div class="particulars-div particulars-age"><i class="'+data.data[0].sex+'"></i><span>'+data.data[0].age+'岁</span></div>'
                                        +'<div class="particulars-div particulars-meili"><span>'+data.data[0].level+'级魅力</span></div>'
                                        +ovip+'</div>';
                            }
                        }
                        page=3;
                        page_num=1;
                        $('.particulars .box').html(ohtml);
                        owight=$('.particulars-one').width();
                        $('.p-img').on('load',function(){
                            if($(this).width()<owight){
                                $(this).addClass('p-imgh');
                            }
                        })//判断宽高
                    }else{
                        opent=true;
                        data.data[0].face?othis.find('.p-img').attr('src',DOMAIN_NAME+data.data[0].face) :othis.find('.particulars-img img').attr('src','images/sherch_bq.png');
                        var otop='<div class="img-amount">'+data.data[0].files_num+'</div>';
                        data.data[0].odynamic ? otop+='<div class="friends"></div>':false;
                        data.data[0].vip ? otop+='<div class="vip"></div>':false;
                        othis.find('.friends-box').html(otop);
                        othis.attr('data-id',data.data[0].uid);
                        
                        othis.find('.img-amount').text(data.data[0].files_num);
                        othis.find('.particulars-name').text(data.data[0].nickname);
                        othis.find('.particulars-age i').attr('class',data.data[0].sex);
                        othis.find('.particulars-age span').text(data.data[0].age);
                        othis.find('.particulars-meili span').text(data.data[0].level+'级魅力');
                        
                    }
                    
                }else if(data.error_code == 1001){  
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    console.log(data)
                    if(page_num==3){
                        $('.particulars .box').html('');
                    }else{
                        if(oerrormsg!='off' ){
                            opent ? page=1:page=2;
                            pagelod(othis)
                        }
                        
                    }
                }
            }
        });
    }
    
    
    /**
     * listlode
     * 加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function listlode(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/searchText",
            dataType:'jsonp',
            data: { uid: uid,token:token,},
            success: function(data) {
                if(data.error_code == 0){
                    var olist ='<h3 class="recommend-title2">大家都在搜</h3>';
                    for(var i=0;i<data.data.length ; i++){ 
                        olist +='<a href="javascript:">'+data.data[i].content+'</a>';
                    }
                    $('.recommend-box').html(olist);
                }else if(data.error_code == 1001){  
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }
        });
    }
    
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */